//获取应用实例
var app = getApp();
var idCard = {
    type:"cardPicture",
    uploadText:"上传手持身份证照片",
    exampleSrc:"https://yzres.oss-cn-shenzhen.aliyuncs.com/wx/images/year/idcard_example@2x.png",
} ;
var policy = {
    type:"policyPicture",
    uploadText:"上传交强险保单副本",
    exampleSrc:"https://yzres.oss-cn-shenzhen.aliyuncs.com/wx/images/year/policy_example@2x.png",
} ;
var btnText = {
    doingUplaodText:"立即上传",
    refreshUploadText:"重新上传"
};
Page({
    data: {
        uploadBtnText:btnText.doingUplaodText,
        uploadText:idCard.uploadText,
        type:idCard.type,
        exampleSrc:idCard.exampleSrc,
        uploadImgSrc:"",
        isShowUploadBtn:true,
        needUpdate:true,
        isShowExample:false,
        isShowRefreshCamera:false,
    },
    onLoad: function (option) {
        var type = option.type;
        // type = "policyPicture";
        var uploadImgSrc = option.uploadImgSrc||'';
        // uploadImgSrc = "https://yzres.oss-cn-shenzhen.aliyuncs.com/wx/images/year/policy_example@2x.png";
        var uploadBtnText = btnText.doingUplaodText;
        var isShowRefreshCamera = false;
        var isShowUploadBtn = true;
        var existUploadImg = (uploadImgSrc!='');
        if(existUploadImg){//如果有上传过图片
            uploadBtnText = btnText.refreshUploadText;
            isShowRefreshCamera = true;
            isShowUploadBtn = false;
        }

        if(type==policy.type){
            this.setData({
                uploadImgSrc:uploadImgSrc,
                uploadBtnText:uploadBtnText,
                uploadText:policy.uploadText,
                type:policy.type,
                exampleSrc:policy.exampleSrc,
                isShowRefreshCamera:isShowRefreshCamera,
                isShowUploadBtn:isShowUploadBtn,
            })
        }else{
            this.setData({
                uploadImgSrc:uploadImgSrc,
                uploadBtnText:uploadBtnText,
                uploadText:idCard.uploadText,
                type:idCard.type,
                exampleSrc:idCard.exampleSrc,
                isShowRefreshCamera:isShowRefreshCamera,
                isShowUploadBtn:isShowUploadBtn,
            })
        }
    },
    //点击相机拍照事件
    cameraViewTap:function () {
        this.chooseImgFn();
    },
    //重新上传拍照事件
    refreshCameraTap:function () {
        this.chooseImgFn();
    },
    uploadTap:function (e) {
        var pageObj = this;
        var uploadImgSrc = pageObj.data.uploadImgSrc;
        if(uploadImgSrc==''){
            app.errorToast("请选择图片！");
            return false;
        }
        var opt = {
            filePath:uploadImgSrc,
            params:{uploadType:'01',owner:app.globalData.openId||'123456'},
            success: function (res) {    //上传成功将数据保存到缓存中
                var dataObj=JSON.parse(res.data);
                if(dataObj.code==1000){
                    if(idCard.type==pageObj.data.type){
                        app.setStorageSync(app.globalData.cardPictureKey,dataObj.body.imageUrl);
                        pageObj.setData({});
                    }else{
                        app.setStorageSync(app.globalData.policyPictureKey, dataObj.body.imageUrl);
                    }
                    if(pageObj.data.needUpdate){
                        app.navigateBack();
                        pageObj.setData({
                            needUpdate: false
                        });
                    }
                }
            },
            fail:function (res) {
            }
        };
        app.uploadFile(opt);
    },
    //预览上传图片
    viewUploadImgTap:function () {
        var uploadImgSrc = this.data.uploadImgSrc;
        var opt = {
            urls:[uploadImgSrc],
            success:function (res) {
                console.log(res);
            }
        };
        app.previewImage(opt)
    },

    exampleTap:function () {
        this.setData({isShowExample: true});
    },
    closeExampleTap:function () {
        this.setData({isShowExample: false});
    },
    chooseImgFn:function () {
        var pageObj = this;
        var opt = {
            count:1,
            sizeType:['compressed'],
            sourceType:['album', 'camera'],
            success: function (res) {
                pageObj.setData({uploadImgSrc: res.tempFilePaths[0],isShowRefreshCamera:true,isShowUploadBtn:true});
            },
            fail: function (res) {
                console.log(res)
            }
        };
        app.chooseImage(opt);
    }
});
